//go:build deprecated
// +build deprecated

package rpcserver

import (
	corev1 "gluenet/internal/agent/api/core/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"

	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/service/kubevirtsvc"
)

const (
	kubevirt = rpcv1.RPC_SERVICE_KUBEVIRT
)

func init() {
	depend.Provide(NewKubevirtRouter)
	registerRouter(kubevirt, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *kubervirtRouter) {
			d.initRouter(c)
		})
	})
}

type kubervirtRouter struct {
	kubevirt *kubevirtsvc.Service
}

func NewKubevirtRouter(svc *kubevirtsvc.Service) *kubervirtRouter {
	return &kubervirtRouter{svc}
}

func (r *kubervirtRouter) initRouter(c rpcserver.IRoutes) {

	c.Group(rpcv1.VIRTUAL_MACHINE_RESOURCE).
		Func(`create`, r.createVirtualMachine).
		Func(`get`, r.getVirtualMachine).
		Func(`delete`, r.deleteVirtualMachine)

	c.Group(rpcv1.VIRTUAL_MACHINE_INSTANCE_RESOURCE).
		Func(`create`, r.createVirtualMachineInstance).
		Func(`get`, r.getVirtualMachineInstance).
		Func(`delete`, r.deleteVirtualMachineInstance)

	c.Group(rpcv1.VIRTUAL_MACHINE_SERVICE_RESOURCE).
		Func(`create`, r.createVirtualMachineService).
		Func(`get`, r.getVirtualMachineService).
		Func(`delete`, r.deleteVirtualMachineService)
}

func (r *kubervirtRouter) createVirtualMachineInstance(c *rpcserver.Context) {
	vritualMachineInstance := new(corev1.KubeVirtVirtualMachineInstance)
	if err := c.BindJSON(vritualMachineInstance); err != nil {
		return
	}

	result := r.kubevirt.CreateVirtualMachineInstance(vritualMachineInstance)
	c.JSON(result)
}

func (r *kubervirtRouter) getVirtualMachineInstance(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}

	result := r.kubevirt.GetVirtualMachineObject(meta)
	c.JSON(result)
}

func (r *kubervirtRouter) deleteVirtualMachineInstance(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}

	result := r.kubevirt.DeleteVirtualMachineInstance(meta)
	c.JSON(result)
}

func (r *kubervirtRouter) createVirtualMachine(c *rpcserver.Context) {
	vritualMachine := new(corev1.KubeVirtVirtualMachine)
	if err := c.BindJSON(vritualMachine); err != nil {
		return
	}

	result := r.kubevirt.CreateVirtualMachine(vritualMachine)
	c.JSON(result)
}

func (r *kubervirtRouter) getVirtualMachine(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}

	result := r.kubevirt.GetVirtualMachineObject(meta)
	c.JSON(result)
}

func (r *kubervirtRouter) deleteVirtualMachine(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}

	result := r.kubevirt.DeleteVirtualMachine(meta)
	c.JSON(result)
}

func (r *kubervirtRouter) createVirtualMachineService(c *rpcserver.Context) {
	vritualMachineService := new(corev1.KubeVirtVirtualMachineService)
	if err := c.BindJSON(vritualMachineService); err != nil {
		return
	}

	result := r.kubevirt.CreateVirtualMachineService(vritualMachineService)
	c.JSON(result)
}

func (r *kubervirtRouter) getVirtualMachineService(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}

	result := r.kubevirt.GetVirtualMachineService(meta)
	c.JSON(result)
}

func (r *kubervirtRouter) deleteVirtualMachineService(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}
	result := r.kubevirt.DeleteVirtualMachineService(meta)

	c.JSON(result)
}
